﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SolidWorks.Interop.sldworks;

namespace SolidworksDLL
{
    public class FileConverterHelp
    {
        public static bool ConvertFile(ModelDoc2 swModel, string path)
        {
            if (swModel == null)
            {
                return false;
            }
            if (path == "")
            {
                return false;
            }

            string ext = Path.GetExtension(path).ToUpper();
            string dir = Path.GetDirectoryName(path);

            Directory.CreateDirectory(dir);

            switch (ext)
            {
                case ".PDF":
                    return ConverPDF(swModel, path, "");

                case ".IGS":
                case ".X_T":
                case ".X_B":
                case ".STEP":
                case ".SAT":
                case ".STL":
                    if (swModel.GetType() == 3)
                    {
                        MessageBox.Show("出现错误：工程图无法转换为3D格式");
                        return false;
                    }
                    return ConverFile(swModel, path, "");

                case ".BMP":
                case ".JPG":
                case ".PNG":
                    return ConverBMP(swModel, path, ext);

                case ".DWG":
                case ".DXF":
                    if (swModel.GetType() == 3)
                    {
                        return ConverFile(swModel, path, ext);
                    }
                    else if (swModel.GetType() == 1)
                    {
                        if (swModel.GetBendState() == 0)
                        {
                            MessageBox.Show("出现错误：非钣金逻辑模型无法输出为DWG/DXF格式");
                            return false;
                        }
                        return ConverDWG(swModel, path, ext);
                    }
                    else
                    {
                        MessageBox.Show("出现错误：装配体模型无法转换为DWG/DXF格式");
                        return false;
                    }

                default:
                    MessageBox.Show("不支持的文件转换格式");
                    return false;
            }
        }

        public static bool ConverFile(ModelDoc2 swModel, string path, string type)
        {
            ModelDocExtension docExtension = swModel.Extension;
            int err = 0;
            int war = 0;
            bool b = docExtension.SaveAs(path, 0, 1, null, ref err, ref war);
            if (err != 0)
            {
                MessageBox.Show("出现错误：" + err.ToString());
            }

            return b;
        }

        public static bool ConverBMP(ModelDoc2 swModel, string path, string type)
        {
            swModel.Extension.RunCommand(332, "");  //使用命令使得图形缩放适应于窗口

            string temp = path.Replace(type, ".BMP");
            bool b = swModel.SaveBMP(temp, 0, 0);

            if (b == false)
            {
                MessageBox.Show("保存图片出现错误");
                return b;
            }

            if (type == ".BMP")
            {
                return b;
            }
            else
            {
                Image image = Image.FromFile(temp);
                if (type == ".JPG")
                {
                    image.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg);
                }
                else if (type == ".PNG")
                {
                    image.Save(path, System.Drawing.Imaging.ImageFormat.Png);
                }
            }

            return b;
        }

        public static bool ConverPDF(ModelDoc2 swModel, string path, string type)
        {
            ModelDocExtension docExtension = swModel.Extension;

            ExportPdfData swExportPDFData = SolidWorksHelp.swApp.GetExportFileData(1);
            swExportPDFData.ViewPdfAfterSaving = false;

            if (swModel.GetType() == 1 || swModel.GetType() == 2)
            {
                swExportPDFData.ExportAs3D = true;
            }
            else
            {
                swExportPDFData.SetSheets(1, null);
            }
            int err = 0;
            int war = 0;
            bool b = docExtension.SaveAs(path, 0, 1, null, ref err, ref war);
            if (err != 0)
            {
                MessageBox.Show("出现错误：" + err.ToString());
            }

            return b;
        }

        public static bool ConverDWG(ModelDoc2 swModel, string path, string type)
        {
            PartDoc swPart = (PartDoc)swModel;

            double[] dataAlignment = new double[12];
            dataAlignment[0] = 0.0;
            dataAlignment[1] = 0.0;
            dataAlignment[2] = 0.0;
            dataAlignment[3] = 1.0;
            dataAlignment[4] = 0.0;
            dataAlignment[5] = 0.0;
            dataAlignment[6] = 0.0;
            dataAlignment[7] = 1.0;
            dataAlignment[8] = 0.0;
            dataAlignment[9] = 0.0;
            dataAlignment[10] = 0.0;
            dataAlignment[11] = 1.0;

            return swPart.ExportToDWG2(path, swModel.GetPathName(), 1, true, dataAlignment, false, false, 13, null);
        }
    }

    /*

     */
}